package com.custom.base.view

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.github.jdsjlzx.ItemDecoration.GridItemDecoration
import com.github.jdsjlzx.ItemDecoration.SpacesItemDecoration

import com.github.jdsjlzx.recyclerview.LRecyclerView
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter
import com.github.jdsjlzx.recyclerview.ProgressStyle
import com.custom.base.R
import com.custom.base.manager.SDActivityManager
import com.custom.base.util.SDViewUtil

/**
 * 作者：
 * 时间：12/15/20
 * 描述：LRecyclerView
 */
class RecyclerListView(context: Context, var attrs: AttributeSet) : RelativeLayout(context, attrs){
    var nData: NullDataView? = null
    var lr: LRecyclerView? = null
    private var header: View? = null
    private var listener:OnPullRefreshListener? = null
    private var type: TYPE = TYPE.LinearLayout//设置布局类型
    private var count: Int//网格布局和瀑布流布局是的纵向个数
    private var itemDecoration: Boolean//是否设置分割线
    private var pullRefreshEnabled:Boolean//是否下啦刷新
    private var loadMoreEnabled:Boolean//是否加载更多功能
    private var itemDecorationHeight:Float//分割线高度
    private var itemDecorationColor:Int//分割线颜色
    private var indicatorColor: Int//头部颜色
    private var hintColor: Int//头部颜色
    private var rLbackgroundColor: Int//头部背景颜色
    private var isNullData = true//是否启用空数据背景

    init {
        val attrTypes: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.RecyclerListView)
        attrTypes.apply {
            when (getInt(R.styleable.RecyclerListView_RecyclerListView_type,0)) {
                0 -> type = TYPE.LinearLayout
                1 -> type = TYPE.GridLayout
                2 -> type = TYPE.StaggeredGridLayout
            }
            count = getInt(R.styleable.RecyclerListView_RecyclerListView_count,0)
            itemDecoration = getBoolean(R.styleable.RecyclerListView_RecyclerListView_itemDecoration,false)
            pullRefreshEnabled = getBoolean(R.styleable.RecyclerListView_RecyclerListView_pullRefreshEnabled,true)
            loadMoreEnabled = getBoolean(R.styleable.RecyclerListView_RecyclerListView_loadMoreEnabled,true)
            itemDecorationHeight = getFloat(R.styleable.RecyclerListView_RecyclerListView_itemDecorationHeight,1f)
            itemDecorationColor = getColor(R.styleable.RecyclerListView_RecyclerListView_itemDecorationColor,Color.parseColor("#5e5656"))
            indicatorColor = getResourceId(R.styleable.RecyclerListView_RecyclerListView_indicatorColor,R.color.colorAccent)
            hintColor = getResourceId(R.styleable.RecyclerListView_RecyclerListView_hintColor,R.color.dark)
            rLbackgroundColor = getResourceId(R.styleable.RecyclerListView_RecyclerListView_backgroundColor,R.color.Transparent)
            isNullData = getBoolean(R.styleable.RecyclerListView_RecyclerListView_nullData,true)
        }
        if(isNullData){
            nData = NullDataView(context,attrs)
            addView(nData,SDViewUtil.layoutParamsLinearLayoutMM)
            nData?.visibility = View.GONE
        }
        lr = LRecyclerView(context,attrs)
        addView(lr,SDViewUtil.layoutParamsLinearLayoutMM)
    }

    /**
     * 初始化
     */
    fun initPullToRefreshListView(
        adapter: RecyclerView.Adapter<*>?,
        listener: OnPullRefreshListener? = null,//刷新回调
        header: View? = null//头部
    ) {
        this.listener = listener
        this.header = header
        lr?.apply {
            val mLRecyclerViewAdapter = LRecyclerViewAdapter(adapter)
            if(header!=null) mLRecyclerViewAdapter.addHeaderView(header)
            this.adapter = mLRecyclerViewAdapter
            //是否自动加载更多功能  设置尾部会失效
            setLoadMoreEnabled(loadMoreEnabled)
            //设置布局
            set(type, count,mLRecyclerViewAdapter,itemDecoration)
            //是否下拉刷新功能
            setPullRefreshEnabled(pullRefreshEnabled)
            //设置分割线
            if (itemDecoration) {
                val divider = GridItemDecoration.Builder(SDActivityManager.instance.lastActivity)
                    .setHorizontal(SDViewUtil.dp2px(itemDecorationHeight))
                    .setVertical(SDViewUtil.dp2px(itemDecorationHeight))
                    .setColor(itemDecorationColor)
                    .build()
                addItemDecoration(divider)
            }
//            //设置尾部，自动加载更多会失效
//            if(!loadMoreEnabled) {
//                //获取尾部
//                val sampleFooter = SampleFooter(SDActivityManager.instance.lastActivity)
//                //点击尾部回调
//                sampleFooter.setOnClickListener { listener?.onPullUpToRefresh(this@RecyclerListView) }
//                //设置上拉刷新进度样式
//                setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader)
//                mLRecyclerViewAdapter.addFooterView(sampleFooter)
//            }
            //设置下拉刷新进度样式
            setRefreshProgressStyle(ProgressStyle.LineSpinFadeLoader)
            //设置头部加载颜色
            setHeaderViewColor(indicatorColor, hintColor, rLbackgroundColor)
            //设置底部加载颜色
            setFooterViewColor(indicatorColor, hintColor, rLbackgroundColor)
            //设置底部加载文字提示
            setFooterViewHint("拼命加载中", "我是有底线的", "网络不给力啊，点击再试一次吧")
            //下拉刷新回调
            setOnRefreshListener { listener?.onPullDownToRefresh(this@RecyclerListView) }
            //上拉加载回调
            setOnLoadMoreListener { listener?.onPullUpToRefresh(this@RecyclerListView) }
            //开始刷新
            refresh()
        }
    }

    /**
     * 改变布局
     * @param type   布局类型
     * @param count
     */
    private fun set(
        type: TYPE,
        count: Int,
        adapter: RecyclerView.Adapter<*>,
        itemDecoration: Boolean = false,//是否设置分割线
        itemDecorationHeight:Float = 1f,//分割线高度
        itemDecorationColor:Int = Color.parseColor("#e5e5e5")//分割线颜色
    ) {
        lr?.apply {
            when (type) {
                TYPE.LinearLayout //设置为线性
                -> this.layoutManager = LinearLayoutManager(SDActivityManager.instance.lastActivity)
                TYPE.GridLayout //设置为网格
                -> {
                    this.layoutManager = GridLayoutManager(SDActivityManager.instance.lastActivity, count)
                    this.adapter = adapter//必须重新setAdapter
                }
                TYPE.StaggeredGridLayout -> { //瀑布流
                    this.layoutManager = StaggeredGridLayoutManager(count, StaggeredGridLayoutManager.VERTICAL)
                    this.adapter = adapter//必须重新setAdapter
                    if(itemDecoration){
                        //设置item之间的间隔
                        val decoration = SpacesItemDecoration.newInstance(itemDecorationHeight.toInt(), itemDecorationHeight.toInt(), count, itemDecorationColor)
                        this.addItemDecoration(decoration)
                    }
                }
            }
        }
    }

    /**
     * 结束刷新
     */
    fun onRefreshComplete() {
        lr?.refreshComplete(10)
    }

    enum class TYPE {
        LinearLayout,
        GridLayout,
        StaggeredGridLayout
    }
    interface OnPullRefreshListener {
        fun onPullDownToRefresh(recyclerListView: RecyclerListView)
        fun onPullUpToRefresh(recyclerListView: RecyclerListView) {}
    }
}
